<!DOCTYPE html>
<html lang="zh">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="/module/bootstrap-3.4.1/css/bootstrap-theme.min.css">
    <link rel="stylesheet" href="/module/bootstrap-3.4.1/css/bootstrap.min.css">
    <link rel="stylesheet" href="/font/font-awesome-4.7.0/css/font-awesome.css">
    <link rel="stylesheet" href="/css/common.css">
    <title>分类</title>
</head>

<body>
    <div id="warp">
        <div class="left">
            <div class="sidebar">
                <div class="navbar navbar-default" id="mycollapse">
                    <!-- <ul class="nav navbar-nav nav-pills nav-stacked"> -->
                    <ul class="nav nav-pills nav-stacked">
                        <li><a href="/index"><i class="fa fa-dashboard fa-fw"></i>首页</a></li>
                        <li><a href="/user"><i class="fa fa-address-card fa-fw"></i>用户</a></li>
                        <li><a href="/subscribe"><i class="fa fa-calendar-times-o fa-fw"></i>预约</a></li>
                        <li><a href="/activity "><i class="fa fa-ship fa-fw"></i>活动</a></li>
                        <li><a href="/integral "><i class="fa fa-line-chart fa-fw"></i>积分</a></li>
                        <li><a href="/opinion"><i class="fa fa-pencil-square fa-fw"></i>意见</a></li>
                        <li><a href="/goods"><i class="fa fa-shopping-cart fa-fw"></i>商品</a></li>
                        <li><a href="/graph"><i class="fa fa-picture-o fa-fw"></i>轮播图</a></li>
                        <li><a href="/scan"><i class="fa fa-header fa-fw"></i>扫描记录</a></li>
                        <li class="active"><a href="/classify"><i class="fa fa-shekel fa-fw"></i>分类</a></li>
                    </ul>
                </div>
            </div>
        </div>
        <div class="right">
            <ol class="breadcrumb">
                <li class="active">分类</li>
            </ol>
            <div class="content">
                <button type="button" class="btn btn-primary" style="margin-bottom: 10px" data-toggle="modal"
                    data-target=".modal">添加分类垃圾</button>
                <div class="modal fade" tabindex="-1" role="dialog">
                    <div class="modal-dialog" role="document">
                        <div class="modal-content">
                            <div class="modal-header">
                                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                                        aria-hidden="true">&times;</span></button>
                                <h4 class="modal-title">添加分类垃圾</h4>
                            </div>
                            <div class="modal-body">
                                <form>
                                    <div class="form-group">
                                        <label class="form-control-static">垃圾类别</label>
                                        <select class="form-control" id="rubbish-type" v-model:value="type"  placeholder="垃圾类别">
                                            <option>可回收垃圾</option>
                                            <option>有害垃圾</option>
                                            <option>厨余垃圾</option>
                                            <option>其他垃圾</option>
                                        </select>
                                    </div>
                                    <div class="form-group">
                                        <label class="form-control-static">垃圾名称</label>
                                        <input type="text" class="form-control" id="rubbish-name"
                                            placeholder="垃圾名称" autocomplete="new-password" v-model:value="name">
                                    </div>
                                </form>
                            </div>
                            <div class="modal-footer">
                                <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                                <button type="button" class="btn btn-primary" @click="submitClick">提交</button>
                            </div>
                        </div><!-- /.modal-content -->
                    </div><!-- /.modal-dialog -->
                </div><!-- /.modal -->

                <div class="panel panel-default">
                    <!-- Default panel contents -->
                    <div class="panel-heading">分类列表</div>
                    <!-- Table -->
                    <table class="table table-bordered table-hover">
                        <tr>
                            <th>名称</th>
                            <th>类别</th>
                            <!-- <th>时间</th> -->
                            <th>操作</th>
                        </tr>
                        <tr v-for="(item,index) in classifyList" :key="index">
                            <td>{{item.name}}</td>
                            <td>{{item.type}}</td>
                            <!-- <td>{{item.time.split('T')[0]}}</td> -->
                            <td>
                                <button type="button" class="btn btn-danger btn-xs" @click="deleteRubbish(item._id)">删除</button>
                            </td>
                        </tr>
                    </table>
                </div>
            </div>
        </div>
    </div>

    <script src="/js/jquery.min.js"></script>
    <script src="/module/bootstrap-3.4.1/js/bootstrap.min.js"></script>
    <script src="/js/vue.js"></script>
    <script src="/js/axios.min.js"></script>
    <script>
        const app = new Vue({
            el: '#warp',
            data: {
                type: '',
                name: '',

                classifyList: [],
            },
            methods: {
                async submitClick() {
                    if (this.name === '' || this.type === '') {
                        alert('数据不能为空')
                    } else {
                        const result = await axios.post('/request/saveClassify', { name: this.name, type: this.type, time: new Date().toISOString() })
                        if (result.status === 200 && result.data.code === 200) {
                            this.getClassify()
                            $(document.querySelector('.content .modal')).modal('hide')
                        } else {
                            alert('添加分类失败!' + result.message)
                        }
                        
                    }
                },
                getClassify() {
                    axios.get('/request/getClassify').then(res => {
                        if (res.status === 200 && res.data.code === 200) {
                            this.classifyList = res.data.classifyList
                        } else {
                            alert('请求分类失败')
                        }
                    })
                },
                deleteRubbish(id) {
                    axios.post('/request/deleteClassify',{id}).then(res => {
                        if (res.status === 200 && res.data.code === 200) {
                            this.getClassify()
                        } else {
                            alert('删除分类失败')
                        }
                    })
                },
            },
            created() {
                this.getClassify()
            },
        })
    </script>
</body>

</html>